/**
 * @class   wosh::BusRing
 * @brief   wosh::Bus implementation, ring-bus architecture.
 *          Messages are posted asynchronously. The internal thread loop
 *          the message queue forwarding the message to each listener.
 *          The order of messages' arrival is predictable.
 *
 *
 * Let's see a worse case which is common while developing and
 * testing new bundles. Assume a deadlock bug of the new service when
 * accessing its internal memory.
 * An user/service send a wosh::Request message for calling the bugged method,
 * the caller thread is owner by the attached wosh::Bus implementation (the message
 * dispatcher).
 * First, the processing won't be done, moreover it will stop the dispatcher thread 
 * and the message won't be delivered to successors.
 * Bus won't work anymore, new subscription/removal will deadlock caller's thread,
 * the message queue will grow undefinately.
 *
 *
 *
 * \msc
Bus,Receiver1,Receiver2, ReceiverN,BusManager;
--- [ label=" LOOP "];
Bus->Bus [label="wait next Message in queue"];
Bus<-Bus [label="pop Message from queue"];
|||;
--- [label=" IF isForMyself(Message) ", URL="\ref wosh::Bus::isForMyself()"];
Bus<-Bus [ label="lockForRead(nodes)" ];
Bus->Receiver1 [label="busMessage()", URL="\ref wosh::IBusListener::busMessage()"];
Receiver1=>Receiver1 [ label="process(Message)" ];
Bus<-Receiver1 [ label="busMessage(END)"];
--- [ label=" iterated for each IBusListener "];
|||;
Bus->Receiver2 [label="busMessage()", URL="\ref wosh::IBusListener::busMessage()"];
Receiver2 rbox Receiver2 [label="process(Message)"];
Bus->ReceiverN [label="busMessage()", URL="\ref wosh::IBusListener::busMessage()"];
ReceiverN rbox ReceiverN [label="process(Message)"];
|||;
Bus<-Bus [label="unLock(nodes)"];
--- [label=" ENDIF isForMyself(Message) "];
|||;
Bus->BusManager [label="forward to BusManager\nBusManager::push_message(Message, Bus)", URL="\ref wosh::Bus::push_message()"];
--- [ label=" LOOP "];
 * \endmsc
 *
 *
 *
 *
 *
 * @see     wosh::BusManager
 ****************************************************************************
 * @version $Id: BusRing.h 2868 2010-08-07 21:13:01Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Core_BusRing_H__
 #define __WOSH_Core_BusRing_H__

 #include <woshDefs.h>
 #include <core/Bus.h>
 #include <core/Containers.h>
 #include <core/Thread.h>
 #include <core/MessageQueue.h>


namespace wosh {

 #define _BusRing_NAME			"wosh.Bus.Ring"

class BusRingManager;

class BusRing :		public wosh::Bus,
					protected wosh::ThreadImpl
 {
	WOSH_BUS(wosh::BusRing)

	public:
		/**
		 * @brief  Empty constructor.
		 * @param [in] busname the name to assign to the bus (usually it's something like 'wosh.Bus.Devices')
		 * @note   Raise the BusManager created (1) event.
		 */
		BusRing( const std::string& busname = "" );
		/**
		 * @brief  Empty destructor.
		 * @note   Raise the BusManager destroying (0) event.
		 */
		virtual ~BusRing();

	public:
		virtual void crashDetected( double probability );
		virtual void setParentObject( const Object* parent );


/** @name Bus interface - management
 * @{
 ******************************************************************************/
	public:
		virtual WRESULT startBus( unsigned long timeout = LIMIT_ULONG_MAX );
		virtual WRESULT stopBus( unsigned long timeout = LIMIT_ULONG_MAX );

		virtual bool isBusRunning() const			{ return Thread::isThreadRunning(); }

//@}
/** @name Internal methods
 * @{
 ******************************************************************************/
	protected:

		/**
		 * @brief  Post a message on the Bus (forward message to handlers).
		 * @param [in] message the message to be forwarded to handlers
		 */
		virtual void forward_message_to_listeners( const Message* message );

		virtual void forward_event_to_listeners( Bus::BUS_STATE state );

		/**
		 * @brief  
		 * @see    
		 */
		virtual void runThread();

//@}
/** @name Internal vars
 * @{
 ******************************************************************************/
	protected:
		mutable BusDebugInfo cDebug;	///< store temporary (real-time) debug info

		friend class wosh::BusManager;	///< let our 'manager' access protected functions

//@}


}; // class def

}; // namespace wosh

#endif //__WOSH_Core_BusRing_H__
